package com.ontheroad.mysql.device.service;

import java.util.Date;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.modoumama.service.base.impl.BaseServiceImpl;

import com.ontheroad.device.model.CentralDeviceDto;
import com.ontheroad.device.model.CentralDeviceEntity;
import com.ontheroad.device.model.DeviceDto;
import com.ontheroad.device.model.DeviceEntity;
import com.ontheroad.device.service.DeviceTopicService;
import com.ontheroad.mysql.device.mapper.CentralDeviceTopicMapper;
import com.ontheroad.mysql.device.mapper.DeviceTopicMapper;
import com.ontheroad.utils.UUIDUtil;

/*import com.ontheroad.utils.SmsUtil;*/

/**
 * Created by kedong on 2017/6/25 0021.
 */
@Service
@Transactional
public class DeviceTopicServiceImpl extends BaseServiceImpl<DeviceDto, String>
		implements DeviceTopicService {

	private Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private DeviceTopicMapper deviceTopicMapper;
	
	@Autowired
	private CentralDeviceTopicMapper centralDeviceTopicMapper;


	public HashMap<String, Object> insertDeviceList(DeviceDto deviceDto)
			throws Exception {
		HashMap<String, Object> bean  =new HashMap<String, Object>();
		log.info("添加设备信息");
		for(DeviceEntity deviceEntity:deviceDto.getDevices()){
			deviceEntity.setId(UUIDUtil.generate());
			deviceEntity.setUpdateTime(new Date());
			deviceEntity.setCreateTime(new Date());
			deviceEntity.setIsEnable(true);
			CentralDeviceEntity centralDeviceEntity=new CentralDeviceEntity();
			centralDeviceEntity.setId(UUIDUtil.generate());
			centralDeviceEntity.setPort(deviceEntity.getPort());
			centralDeviceEntity.setDeviceId(deviceEntity.getId());
			centralDeviceEntity.setType(deviceEntity.getType());
			centralDeviceEntity.setCmd(deviceDto.getCmd());
			centralDeviceEntity.setChannel(deviceDto.getChannel());
			centralDeviceEntity.setMac(deviceDto.getMac());
			centralDeviceEntity.setIsSub(deviceDto.getIsSub());
			centralDeviceEntity.setIsEnable(true);
			centralDeviceEntity.setCreateTime(new Date());
			centralDeviceEntity.setUpdateTime(new Date());
			centralDeviceTopicMapper.insertModel(centralDeviceEntity);
		}
		deviceTopicMapper.insertModel(deviceDto.getDevices());
		bean.put("code", 0);
		bean.put("message", "success");
		return bean;
	}

	public HashMap<String, Object> modifyDevice(CentralDeviceDto centralDeviceDto)
			throws Exception {
		HashMap<String, Object> bean  =new HashMap<String, Object>();
		log.info("修改设备信息");
		String deviceId=centralDeviceTopicMapper.findDeviceId(centralDeviceDto);
		DeviceEntity deviceEntity=new DeviceEntity();
		deviceEntity.setId(deviceId);
		deviceEntity.setName(centralDeviceDto.getName());
		deviceEntity.setType(centralDeviceDto.getType());
		deviceEntity.setRoom(centralDeviceDto.getRoom());
		deviceTopicMapper.modifyDevice(deviceEntity);
		bean.put("code", 0);
		bean.put("message", "success");
		bean.put("cmd", centralDeviceDto.getCmd());
		bean.put("isSub", centralDeviceDto.getIsSub());
		bean.put("channel", centralDeviceDto.getChannel());
		bean.put("mac", centralDeviceDto.getMac());
		bean.put("port", centralDeviceDto.getPort());
		bean.put("name", centralDeviceDto.getName());
		bean.put("type", centralDeviceDto.getType());
		bean.put("room", centralDeviceDto.getRoom());
		return bean;
	}
}
